package com.example.pingze.service;

import com.example.pingze.dto.PoemAndPoetInfoDto;
import com.example.pingze.entity.PoemInfo;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.PoemInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class PoemInfoService {

    @Autowired
    private PoemInfoRepository repository;

    public List<PoemInfo> findPoemInfoList(PoemInfo poemInfo) {
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (!StringUtils.isEmpty(poemInfo.getDynasty())) {
                    predicates.add(criteriaBuilder.like(root.get("dynasty"), "%" + poemInfo.getDynasty() + "%"));
                }
                if (!StringUtils.isEmpty(poemInfo.getWriter())) {
                    predicates.add(criteriaBuilder.like(root.get("writer"), "%" + poemInfo.getWriter() + "%"));
                }
                if (!StringUtils.isEmpty(poemInfo.getTypes())) {
                    predicates.add(criteriaBuilder.like(root.get("types"), "%" + poemInfo.getTypes() + "%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return repository.findAll(specification);
    }

    public List<PoemInfo> findPoemInfoByWriter(String writer) {
        return repository.findPoemInfoByWriter(writer);
    }

    public PoemInfo findPoemInfoByDynasty(String value) {
        return repository.findPoemInfoByDynasty(value);
    }

    public PoemAndPoetInfoDto findAllById(String id) {
        PoemAndPoetInfoDto info = repository.findAllById(id);
        return info;
    }

    public List<String> getWriter() {
        List<String> list = repository.findWriter();
        return list;
    }

    public List<PoemInfo> findPoemByTitle(String title) {
        return repository.findPoemInfoByTitleContains(title);
    }

    //多条件查询＆分页
    public Page getPoemInfoList(PoemInfo poemInfo, Pageable pageable) {
        Specification<PoemInfo> specification = new Specification<PoemInfo>() {
            @Override
            public Predicate toPredicate(Root<PoemInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(poemInfo.getTitle())){
                    predicates.add(criteriaBuilder.equal(root.get("title"),poemInfo.getTitle()));
                }
                if(!StringUtils.isEmpty(poemInfo.getDynasty())){
                    predicates.add(criteriaBuilder.equal(root.get("dynasty"),poemInfo.getDynasty()));
                }
                if(!StringUtils.isEmpty(poemInfo.getWriter())){
                    predicates.add(criteriaBuilder.equal(root.get("writer"),poemInfo.getWriter()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }

    public PoemInfo add(String title, String dynasty, String writer, String type, String content, String remark, String translation, String appreciation) {
        if(StringUtils.isEmpty(title)){
            throw new PingZeException("诗名不能为空");
        }
        if(StringUtils.isEmpty(dynasty)){
            throw new PingZeException("朝代不能为空");
        }
        if(StringUtils.isEmpty(writer)){
            throw new PingZeException("作者不能为空");
        }
        if(StringUtils.isEmpty(type)){
            throw new PingZeException("类型不能为空");
        }
        if(StringUtils.isEmpty(content)){
            throw new PingZeException("正文不能为空");
        }
        if(StringUtils.isEmpty(remark)){
            throw new PingZeException("注释不能为空");
        }
        if(StringUtils.isEmpty(translation)){
            throw new PingZeException("翻译不能为空");
        }
        if(StringUtils.isEmpty(appreciation)){
            throw new PingZeException("不能赏析为空");
        }
        PoemInfo add=new PoemInfo();
        add.setId(UUID.randomUUID().toString());
        add.setTitle(title);
        add.setDynasty(dynasty);
        add.setWriter(writer);
        add.setTypes(type);
        add.setContent(content);
        add.setRemark(remark);
        add.setTranslations(translation);
        add.setAppreciation(appreciation);
        repository.save(add);
        return add;
    }

    public PoemInfo edit(String id, String title, String dynasty, String writer, String type, String content, String remark, String translation, String appreciation) {
        PoemInfo updater = repository.findPoemInfoById(id);
        if(!updater.getTitle().equals(title)){
            updater.setTitle(title);
        }
        if(!updater.getDynasty().equals(dynasty)){
            updater.setDynasty(dynasty);
        }
        if(!updater.getWriter().equals(writer)){
            updater.setWriter(writer);
        }
        if(!updater.getTypes().equals(type)){
            updater.setTypes(type);
        }
        if(!updater.getContent().equals(content)){
            updater.setContent(content);
        }
        if(!updater.getRemark().equals(remark)){
            updater.setRemark(remark);
        }
        if(!updater.getTranslations().equals(translation)){
            updater.setTranslations(translation);
        }
        if(!updater.getAppreciation().equals(appreciation)){
            updater.setAppreciation(appreciation);
        }
        repository.save(updater);
        return updater;
    }

    public PoemInfo findPoemInfoById(String id) {
        return repository.findPoemInfoById(id);
    }

    public void delete(PoemInfo poemInfo) {
        repository.delete(poemInfo);
    }

    public PoemInfo getPoemInfoByWriterAndTitleContains(String writer, String title) {
        return repository.findPoemInfoByWriterAndTitleContains(writer, title);
    }
}
